package com.excesys;

import cn.hutool.core.lang.UUID;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Data
@NoArgsConstructor
@AllArgsConstructor
class Car {
    String name;
    List<Engine> engines;
}

@Data
@NoArgsConstructor
@AllArgsConstructor
class Engine {
    String name; // 假设这是你要过滤的属性
    String status; // 假设这是你要过滤的属性
    Double power; // 假设这是你要过滤的属性
}

public class FilteringNestedCollections {

    public static void main(String[] args) {
        List<Car> cars = new ArrayList<>();
        cars.add(new Car("Benz", Arrays.asList(new Engine(UUID.randomUUID().toString(), "active", 1.5), new Engine(UUID.randomUUID().toString(), "inactive", 2.0))));
        cars.add(new Car("Bmw", Arrays.asList(new Engine(UUID.randomUUID().toString(), "active", 1.5), new Engine(UUID.randomUUID().toString(), "inactive", 2.0))));
        cars.add(new Car("Audi", Arrays.asList(new Engine(UUID.randomUUID().toString(), "active", 1.5), new Engine(UUID.randomUUID().toString(), "inactive", 2.0))));
        cars.add(new Car("Mi", Arrays.asList(new Engine(UUID.randomUUID().toString(), "inactive", 1.5), new Engine(UUID.randomUUID().toString(), "inactive", 2.0))));
        // 初始化cars列表，包含若干Car对象及其内部的Engine列表...
        // 定义过滤条件，例如只保留包含至少一个Engine其status为"active"的Car
        List<Car> filteredCars = cars.stream().filter(car -> car.getEngines().stream().anyMatch(engine -> "active".equals(engine.getStatus()))).collect(Collectors.toList());
        filteredCars.forEach(System.out::println);
    }

}
